package com.example.test.inteceptor;

import java.util.Collection;
import java.util.Properties;

import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMap;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.Configuration;
import org.springframework.stereotype.Component;

@Intercepts(@Signature(type=Configuration.class,method="getMappedStatement",args= {String.class}))
@Component
public class MappedStatementInterceptor implements Interceptor{

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		invocation.getArgs();
		Configuration con = (Configuration) invocation.getTarget();
		Collection<MappedStatement> mappedStatements = con.getMappedStatements();
		mappedStatements.forEach(ms -> {
			ParameterMap pm = ms.getParameterMap();
			pm.getParameterMappings().forEach(ss -> {
				Class<?> javaType = ss.getJavaType();
				String property = ss.getProperty();
				System.out.println("javaType:"+javaType+"....property:"+property);
			});
		});
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
	}

}
